package com.sinogrand.elevator.repository

import android.content.Context
import android.util.Log
import com.sinogrand.elevator.data.models.ProjectUserListResponse
import com.sinogrand.elevator.network.NetworkClient
import com.sinogrand.elevator.utils.LogTags
import com.google.gson.JsonObject

/**
 * 项目用户仓库类
 */
class ProjectUserRepository(context: Context) {
    private val apiService = NetworkClient.getApiService(context)

    /**
     * 获取项目用户列表
     */
    suspend fun getProjectUserList(
        projectId: Int,
        page: Int = 1,
        size: Int = 20,
        name: String? = null
    ): Result<ProjectUserListResponse> {
        return try {
            val requestBody = JsonObject().apply {
                addProperty("project_id", projectId)
                addProperty("page", page)
                addProperty("size", size)
                name?.let { addProperty("name", it) }
            }
            Log.d(LogTags.PROJECT_USER_LIST, "请求项目用户列表: $requestBody")
            val response = apiService.getProjectUserList(requestBody)
            if (response.code == 0 && response.data != null) {
                Log.d(LogTags.PROJECT_USER_LIST, "成功获取项目用户列表: ${response.data.list.size} 条")
                Result.success(response.data)
            } else {
                Log.e(LogTags.PROJECT_USER_LIST, "获取项目用户列表失败: ${response.msg}")
                Result.failure(RuntimeException(response.msg))
            }
        } catch (e: Exception) {
            Log.e(LogTags.PROJECT_USER_LIST, "获取项目用户列表异常: ${e.message}", e)
            Result.failure(e)
        }
    }
}
